﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class InventoryController : Controller
    {
        //
        // GET: /Inventory/
        private readonly IGodownentryRepository _godownentryRepository;
        private readonly IDeliveryorderRepository _deliveryorderRepository;
        private readonly IProductRepository _productRepository;
        private readonly IInventoryRepository _inventoryRepository;
        private readonly IWarehouseRepository _warehouseRepository;
        public InventoryController(IDeliveryorderRepository deliveryorderRepository,IGodownentryRepository godownentryRepository, IWarehouseRepository warehouseRepository, IProductRepository productRepository, IInventoryRepository inventoryRepository)
        {
            _deliveryorderRepository = deliveryorderRepository;
            _productRepository = productRepository;
            _inventoryRepository = inventoryRepository;
            _warehouseRepository = warehouseRepository;
            _godownentryRepository = godownentryRepository;
        }

        public IList<WarehouseModel> GetAllWarehouse()
        {
            var warehouse = _warehouseRepository.GetAll().AsQueryable();
            IList<WarehouseModel> warehousemodels =
                (
                    from w in warehouse
                    select new WarehouseModel
                    {
                        WarehouseID = w.ID,
                        Name = w.Name,
                        Employee = w.Employee.Name,
                    }
                ).ToList();
            return warehousemodels;
        }

        public IList<ProductModel> GetAllproductmodel()
        {
            var product = _productRepository.GetAll().AsQueryable();
            IList<ProductModel> productModels =
                (from p in product
                 select new ProductModel
                 {
                     ID = p.ID,
                     Name = p.Name,
                    
                     ProductCategoy = p.ProductCategory.Name,
                 }
                ).ToList();
            return productModels;
        }
        [GridAction]
        public ActionResult Index()
        {
            return View(new GridModel(GetAllWarehouse()));
        }
        [GridAction]
        public ActionResult WarehouseHierarchyAjax()
        {
            return View(new GridModel(GetAllWarehouse()));
        }
        [GridAction]
        public ActionResult ProductDetailAjax(int productID)
        {
            var product = _productRepository.GetMany(p => p.ID == productID).ToList();
            var productin = _godownentryRepository.GetMany(x=>x.Product.ID==productID).ToList();
            var productout = _deliveryorderRepository.GetMany(x => x.Product.ID == productID).ToList();
            var productdetail = from i in productin
                                from o in productout
                                from p in product
                                where i.Product.ID == o.Product.ID && p.ID==o.Product.ID
                                select new ProductOfwarehouse
                                           {
                                               ProductID = p.ID,
                                               ProductName = p.Name,
                                               Inprice = i.Stockprice,
                                               Intime = i.Datetime,
                                               Countin = i.Count,
                                               Employeein = i.employee.Name,
                                               Outprice = o.Outprice,
                                               Countout = o.Count,
                                               Outtime = o.time,
                                               Employeeout = o.Employee.Name,
                                           };
            return View(new GridModel(productdetail));
        }
        [GridAction]
        public ActionResult ProductIninventory(int warehouseID)
        {
            var product = _godownentryRepository.GetMany(x => x.Warehouse.ID == warehouseID).ToList();
            var inventories = _inventoryRepository.GetAll().ToList();
           
            var pinventory = from p in product
                             from i in inventories
                             where p.Product.ID==i.Product.ID 
                             select new ProductInInventory
                             {
                                 Pid = p.Product.ID,
                                 ProductName = p.Product.Name,
                                 Productcategory = p.Product.ProductCategory.Name,
                                 Productprovider = p.ProductProvider.Name,
                                 Countinventory = i.Count,
                                
                             };
            return View(new GridModel(pinventory));
        }
    }
}
